import { Message, Loading } from 'element-ui';
export const startJzPrinter = (printData)=>{
    console.log(printData)
    if(printData && printData.length){
        // printData = [
        //     {id: '111111', url:'https://erj.csin.net.cn/?signName=88.911.51016988731875054/frank001'},
        //     {id: '222222222', url:'https://erj.csin.net.cn/?signName=88.911.51016988731875054/frank001'},
        //     {id: '333333333', url:'https://erj.csin.net.cn/?signName=88.911.51016988731875054/frank001'},
        // ]
        // printData = [
        //   {id: '111111', url:'wwwwwwwwwwwwwwwwww'},
        //   {id: '222222222', url:'kkkkkkkkkkkkkkk'},
        //   {id: '333333333', url:'lllllllllllllllllllll'},
        // ]
        getInstance();

        setTimeout(()=>{
            initSdk({"fontDir": ""},(function(initres){
                getAllPrinters(function(getRes){
                    if (isJSON(getRes.resultAck.info)) {
                        let allPrinters = JSON.parse(getRes.resultAck.info);
                        let allPrintersName = Object.keys(allPrinters);
                        let allPrintersValue = Object.values(allPrinters);
                        selectPrinter(allPrintersName[0], parseInt(allPrintersValue[0]), (() => {})); //打印的回调不好使
                        setTimeout(()=>{
                            startPrintJob(printData);
                        },2000)
                    }else{
                      Message.warning('打印机未正确连接');
                    }
                })
            }))

        },2000)
    }
}

export const startPrintJob = (printData)=>{
    console.log('重要数据',printData.length)
    let pageIndex = 0;
    let paperType = '1';//纸张类型 1间隙纸 2黑标纸 3连续纸 4定孔纸 5透明纸 6标牌
    let pageCount = printData.length+''; //页数(每次打印的页数？)
    let quantityCount = '1'; //打印份数，建议填写2-100，单份打印时无效(每页重复的次数)
    let density ='3'; //打印浓度
    let printMode = '2'; //打印模式，1--热敏，2--热转印(z401)

    // 初始化画板
    InitDrawingBoard({
      "width": 50,
      "height": 30,
      "rotate": 0,
      "path": "ZT001.ttf",
      "verticalShift": 0,
      "HorizontalShift": 0
    }, (data1 => {
      // 添加文字
      DrawLableTextApiF({
        "x": 0,
        "y": 0,
        "height": 4,
        "width": 50,
        "value": printData[0].id,
        "fontFamily": "宋体",
        "rotate": 0,
        "fontSize": 2.0,
        "textAlignHorizonral": 1,
        "textAlignVertical": 1,
        "letterSpacing": 1.0,
        "lineSpacing": 1.0,
        "lineMode": 6,
        "fontStyle": [false, false, false, false]
      }, (data2 => {
        DrawLableQrApiF({
          "x": 10,
          "y": 5,
          "height": 24,
          "width": 24,
          "value": printData[0].url,
          "rotate": 0,
          "codeType": 31,
        }, (data3 => {
          console.log("数据提交之前")
          // 提交数据
          var totalCount = parseInt(pageCount) * parseInt(quantityCount);
          startJob(parseInt(density), parseInt(paperType), parseInt(printMode), totalCount, function (data) {
            var arrParse = JSON.parse(JSON.stringify(data));
            if (arrParse.resultAck.result != 0) {
              console.log('打印失败！');
              return;
            }
            var intPageCount = parseInt(pageCount);
            var intQuantityCount = parseInt(quantityCount);
            sendCommitJob({}, pageIndex, intPageCount, intQuantityCount,printData);
          });
        }))
      }))
    }))
  }

  function sendCommitJob(printParam, pageIndex, pageCount, quantityCount,printData) {
    console.log('初始的pageIndex=',pageIndex)
    var jsonObj = { "printerImageProcessingInfo": { "printQuantity": quantityCount } };
    commitJob(null, JSON.stringify(jsonObj), function (data) {
      var arrParse = JSON.parse(JSON.stringify(data));
      var resultInfo = "commitJob ok";
      if (arrParse.resultAck.printQuantity == pageCount && arrParse.resultAck.onPrintPageCompleted == quantityCount) {
        endJob(function (data) {
          var arrParse = JSON.parse(JSON.stringify(data));
          if (String(arrParse.resultAck.info).indexOf("endJob ok") > -1) {
            // document.getElementById("printId").disabled = false;
          }
        });
        return;
      }
      if (arrParse.resultAck.errorCode != 0 && (arrParse.resultAck.cacheStatus == "cancel")) {
        // document.getElementById("printId").disabled = false;
        return;
      }

      if (arrParse.resultAck.errorCode != 0 && (arrParse.resultAck.cacheStatus == "pause")) {
        // cancleJobApi();
        return;
      }

      if (String(arrParse.resultAck.info).indexOf(resultInfo) > -1 && (pageIndex+1) != pageCount) {
        setTimeout(function () {
          // 初始化画板
          InitDrawingBoard({
            "width": 50,
            "height": 30,
            "rotate": 0,
            "path": "ZT001.ttf",
            "verticalShift": 0,
            "HorizontalShift": 0
          }, (data1 => {
            // 添加文字
            DrawLableTextApiF({
              "x": 0,
              "y": 0,
              "height": 4,
              "width": 50,
              "value": printData[pageIndex+1].id,
              "fontFamily": "宋体",
              "rotate": 0,
              "fontSize": 2.0,
              "textAlignHorizonral": 1,
              "textAlignVertical": 1,
              "letterSpacing": 1.0,
              "lineSpacing": 1.0,
              "lineMode": 6,
              "fontStyle": [false, false, false, false]
            }, (data2 => {
              DrawLableQrApiF({
                "x": 10,
                "y": 5,
                "height": 24,
                "width": 24,
                "value": printData[pageIndex+1].url,
                "rotate": 0,
                "codeType": 31,
              }, (data3 => {
                sendCommitJob({}, pageIndex+1, pageCount, quantityCount,printData);
              }))
            }))
          }))// 提交数据
        }, 500);
      }
    });
  }

  
// 写文本
const DrawLableTextApiF = (drawLablelJson, callbackFunction)=>{
    DrawLableText(drawLablelJson, callbackFunction);
}
// 打印二维码
const DrawLableQrApiF = (drawLableQrJson, callbackFunction)=>{
    DrawLableQrCode(drawLableQrJson, callbackFunction);
}